import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_bilibili/core/extension/int_extension.dart';
import 'package:flutter_bilibili/core/model/video_model.dart';
import 'package:flutter_bilibili/ui/pages/video_play/video/video_play_related_video_item.dart';
import 'package:flutter_bilibili/ui/pages/video_play/video/video_play_video_info.dart';
import 'package:flutter_bilibili/ui/shared/app_theme.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import '../../../../core/model/archive_related_model.dart';
import '../../../../core/service/request/video_play_request.dart';
import '../dan_mu/dan_mu.dart';

class HYVideoPlayProfile extends StatefulWidget {
  HYVideoModel video;

  HYVideoPlayProfile(this.video);

  @override
  State<HYVideoPlayProfile> createState() => _HYVideoPlayProfileState();
}

class _HYVideoPlayProfileState extends State<HYVideoPlayProfile> {
  int pageIndex = 1;
  int pageSize = 10;
  late List<HYVideoModel> relatedVideos = [];
  late List<HYVideoModel> allRelatedVideos = []; //最多40条，这个api没有分页的设置，就全加载进来
  @override
  void initState() {
    HYVideoRequestRequest.getArchiveRelatedData(widget.video.aid).then((value) {
      if (mounted)
        setState(() {
          allRelatedVideos.addAll(value);
          relatedVideos
              .addAll(allRelatedVideos.sublist(0, pageIndex * pageSize));
        });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      onLoad: () async {
        await Future.delayed(Duration(seconds: 1)).then((value) {
          if (mounted)
            setState(() {
              //每次获取新的4条数据
              relatedVideos.addAll(allRelatedVideos.sublist(
                  pageIndex * pageSize, (++pageIndex) * pageSize));
            });
        });
      },
      child: Container(
        color: Colors.white,
        child: SingleChildScrollView(
          child: Column(
            children: [
              buildVideoPlayVideoInfo(widget.video),
              relatedVideos.isNotEmpty
                  ? buildHYVideoPlayRelatedVideos()
                  : Center(
                      child: Container(
                        width: 30.px,
                        height: 30.px,
                        child: CircularProgressIndicator(
                          color: HYAppTheme.norTextColors,
                        ),
                      ),
                    ),
            ],
          ),
        ),
      ),
    );
  }

  /**
   * relatedVideos要放在widget里，而不是state里，否则这里的的length始终为0
   */
  Widget buildHYVideoPlayRelatedVideos() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.px, vertical: 20.px),
      child: GridView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemCount: relatedVideos.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, childAspectRatio: 0.9),
        itemBuilder: (ctx, index) {
          return HYVideoPlayRelatedVideoItem(relatedVideos[index]);
        },
      ),
    );
  }
}
